package com.itheima.tliasmanagementspringboot.service.impl;

import com.itheima.tliasmanagementspringboot.mapper.DeptMapper;
import com.itheima.tliasmanagementspringboot.mapper.EmpMapper;
import com.itheima.tliasmanagementspringboot.pojo.Dept;
import com.itheima.tliasmanagementspringboot.pojo.DeptLog;
import com.itheima.tliasmanagementspringboot.service.DeptLogService;
import com.itheima.tliasmanagementspringboot.service.DeptService;
import com.itheima.tliasmanagementspringboot.service.EmpService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;
    @Autowired
    EmpMapper empMapper;
    @Autowired
    DeptLogService deptLogService;

    /**
     * 查询所有部门信息
     * @return 查询到的部门信息的集合
     */
    @Override
    public List<Dept> queryAllDeptService() {
        return deptMapper.queryAllDeptMapper();
    }

    /**
     * 功能: 根据id查询部门信息
     * @return 查询到的相应的部门信息
     */
    @Override
    public Dept queryDeptByConditionService(Integer id) {
        return deptMapper.queryDeptByConditionMapper(id);
    }

    /**
     * 功能: 根据id删除部门信息
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)//还是要抛,所以下面那个更方便
    @SneakyThrows//本质就是try catch(编译后就会添加上相关代码),上面加了(rollbackFor = Exception.class)就不用这个了
    public void deleteDeptById(Integer id) {
        //部门下有员工就不能删除,抛异常即可
        //传递的是部门的id
        /*if (empMapper.queryIsExistsEmpInDept(id) != 0) {
            throw new RuntimeException("本部门下还有员工,不能删除部门!");
        }else {
            deptMapper.deleteDeptById(id);
        }*/
        //3.记录事务日志(需要在外部事务前执行,如果放在后面,外部事务代码出现异常,外部事物会回滚,就没法执行该语句(事务了))
        DeptLog deptLog = new DeptLog(null, LocalDateTime.now(), "删除" + id + "部门");
        deptLogService.insert(deptLog);
        //不用判断是否有员工,直接连员工一起删除
        //1.删除部门
        deptMapper.deleteDeptById(id);
        //int i = 1 / 0;//加了事务管理后,能保证一个事务必须全部完成或者全部失败,此处报异常,代表只能都失败,事务回滚到该方法执行前状态
        //new FileInputStream("");
        //2.删除员工
        empMapper.deleteEmpByDeptId(id);



    }

    /**
     * 功能: 添加/插入一条部门信息
     *
     * @param dept
     */
    @Override
    public void insertDeptService(Dept dept) {
        if (deptMapper.queryDeptNameByNameMapper(dept.getName()) != 0) {
            throw new RuntimeException("部门名字重复!");
        }
        //补全一些基础字段的信息(如:时间,密码,状态等等)
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDeptMapper(dept);
    }

    /**
     * 功能:根据id更新部门信息
     * @param dept :部门id
     */
    @Override
    public void updateDeptByIdService(Dept dept) {
        if (deptMapper.queryDeptNameByNameMapper(dept.getName()) != 0) {
            throw new RuntimeException("部门名字重复!");
        }
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDeptByIdMapper(dept);
    }
}
